#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    bool CheckEnd(vector<int>& nums, vector<vector<int>>& queries, int stop)
    {
        int n = nums.size(), change = 0;
        vector<int> diff(n + 1, 0);
        for (int i = 0; i < stop; i++)
        {
            int start = queries[i][0], end = queries[i][1], val = queries[i][2];
            diff[start] += val;
            diff[end + 1] -= val;
        }
        for (int i = 0; i < n; i++)
        {
            change += diff[i];
            if (change < nums[i])
                return false;
        }
        return true;
    }
    int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
        int left = 0, right = queries.size();
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            bool flag = CheckEnd(nums, queries, mid);
            if (flag)
                right = mid - 1;
            else
                left = mid + 1;
        }
        return left > queries.size() ? -1 : left;
    }
};